package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayMessageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李旭
 * @date 2021/10/25 14:20
 * @Description:
 *
 * 发消息
 */
@RestController
@RequestMapping("/mq")
public class SendMessageController {

/*    //生产者 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;//交换机找 RabbitTemplate 怎么找*/
    @Autowired
    private RabbitService rabbitService;//自定义的实现类 简化开发

    //发消息
/*    @GetMapping("/sendMessage/{message}")
    public Result sendMessage(@PathVariable String message){
        //此种发消息  造成消息丢失  无法应答  开启确认应答模式  应答没有回调方法  无法接收应答的消息
        // 像下面这种发消息的方式 根本就不行
        rabbitTemplate.convertAndSend("exchange123","routingKey123",message);
        //封装大量内容
        return Result.ok();
    }*/
    //控制层 Service 业务层
    @GetMapping("/sendMessage/{message}")
    public Result sendMessage123(@PathVariable String message){
        rabbitService.sendMessage("exchange123","routingKey123",message);
        //封装大量内容
        return Result.ok();
    }

    //发送 基于死信的延迟消息
    @GetMapping("/sendDeadLetterMessage/{message}/{ttl}")
    public Result sendDeadLetterMessage(@PathVariable String message,@PathVariable int ttl){
        rabbitService.sendDeadLetterMessage(DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1,message,ttl);
        //封装大量内容
        return Result.ok();
    }

    //发送 基于插件的延迟消息
    @GetMapping("/sendDelayedMessage/{message}/{delayTime}")
    public Result sendDelayedMessage(@PathVariable String message,@PathVariable int delayTime){
        rabbitService.sendDelayedMessage(DelayMessageConfig.exchange_delay,
                DelayMessageConfig.routing_delay,message,delayTime);
        return Result.ok();
    }
}
